// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata mkt_d_ind_con.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/mkt_d_ind_con_log, replace text

clear all
cd /projects/

di "$S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/mkt_d_ind_con/"
noi capture mkdir ${dir_outf}

global year1 = 1977
global year2 = 2013

global gl_perc = "10"
local l_perc = "10"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

global lvmkt = "est msa1983 msa1983cz zipcode fips msa czone"
global vmkt_sel = "msa1983"

//==============================================================================
// Y = Industry concentration
/*
iperc = affects ind_sum_d_sel and w_emps files
y y_lab = dependent variables
fig_rt = root of figure names
p_fig = 0 = skip figures
*/

capture program drop fig_sum
program fig_sum

	args iperc y y_lab fig_rt p_fig
	
	preserve 
	merge 1:1 ch_ind using ${dir_proj}/data/ind_sum_d_sel_`iperc'_${year1}_${year2}
	*Merge with SV weights (2013-1977); i.e. updated weights.
	merge 1:1 ch_ind using ${dir_out}/sv_weights/sv_weights_1977_2013.dta, assert(match) nogen keepusing(w_sv* w_num*)
	local vwgt = "w_sv_`iperc'"
    
foreach vmkt in $lvmkt {
	
	//local vmkt = "msa"
	//local y = "ln_emp_aux" 
	local x = "ln_mu_mkt_`vmkt'_d"
	//local y_lab = `"{&Delta} ln(Auxiliary Employment)"'
	local x_lab = `"{&Delta} ln(Average # of `=upper("`vmkt'")' of All Firms)"'
	
	if "`p_fig'" != "0" {
	twoway (scatter `y' `x', msize(vsmall)) ///
		(lpolyci `y' `x' [aw=`vwgt'], level(99) lcolor(maroon) lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(off) note("99% CI")
		
	//graph export ${dir_outf}/def`idef'/Y_l_emp_aux_d_X_l_mkt_d_`vmkt'.png, replace width(3000) height(2000)
	graph export ${dir_outf}/`fig_rt'_`vmkt'.png, replace width(3000) height(2000)
	}
    
    reg `y' `x' [aw=`vwgt']
    f_reg_append "fr_reg" "`x'"

}

    //local y = "ln_emp_aux" 
	local x1 = "ln_mu_mkt_est_d"
    local x2 = "ln_mu_mkt_fips_d"
    local x3 = "ln_mu_mkt_msa_d"
    local x4 = "ln_mu_mkt_zip_d"
    local x5 = "ln_mu_mkt_czone_d"
	local x6 = "ln_mu_mkt_msa1983cz_d"
	//local y_lab = `"{&Delta} ln(Auxiliary Employment)"'
	local x_lab = `"{&Delta} ln(Average # of Markets of All Firms)"'
	
	/*
	twoway (lpoly `y' `x1' [aw=`vwgt'], lw(med) fi(inten0)) ///
        (lpoly `y' `x2' [aw=`vwgt'], lw(med) fi(inten0)) ///
        (lpoly `y' `x3' [aw=`vwgt'], lw(med) fi(inten0)) ///
        (lpoly `y' `x4' [aw=`vwgt'], lw(med) fi(inten0)) ///
        (lpoly `y' `x5' [aw=`vwgt'], lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(order(1 "Est" 2 "FIPS" 3 "MSA" 4 "ZIP" 5 "CZONE") rows(1)) ///
		note("99% CI")
     
    graph export ${dir_outf}/z_`fig_rt'_all.png, replace width(3000) height(2000)
	*/
    
	if "`p_fig'" != "0" {
    twoway (lpoly `y' `x1' [aw=`vwgt'], lw(med) fi(inten0)) ///
        (lpoly `y' ln_mu_mkt_${vmkt_sel}_d [aw=`vwgt'], lw(med) fi(inten0)), ///
		ytitle("`y_lab'") xtitle("`x_lab'") ///
		legend(order(1 "Est" 2 "${vmkt_sel}") rows(1)) ///
		note("99% CI")
     
    graph export ${dir_outf}/z_`fig_rt'_sel.png, replace width(3000) height(2000)
	}
	
	restore
end

//==============================================================================
//==============================================================================

f_reg_create "fr_reg" "1"

//==============================================================================
// Emp and Pay

do ${dir_do}/mkt_d_ind_0.do "geo"

fig_sum "10" "ln_emps_ind_d_10" `"{&Delta} ln(Top 10% / Ind Emp)"' "Y_ln_emps_ind_d_10_X_ln_mkt_d"
fig_sum "10" "ln_pays_ind_d_10" `"{&Delta} ln(Top 10% / Ind Pay)"' "Y_ln_pays_ind_d_10_X_ln_mkt_d"

fig_sum "1" "ln_emps_ind_d_1" `"{&Delta} ln(Top 1% / Ind Emp)"' "Y_ln_emps_ind_d_1_X_ln_mkt_d"
fig_sum "1" "ln_pays_ind_d_1" `"{&Delta} ln(Top 1% / Ind Pay)"' "Y_ln_pays_ind_d_1_X_ln_mkt_d"

fig_sum "10" "hhi_ind_d" `"{&Delta} HHI"' "Y_hhi_ind_d_X_ln_mkt_d"
fig_sum "10" "ln_hhi_ind_d" `"{&Delta} ln(HHI)"' "Y_ln_hhi_ind_d_X_ln_mkt_d"

//==============================================================================
// Sales

do ${dir_do}/mkt_d_ind_0.do "geo" "" "sales"

fig_sum "10" "ln_saless_ind_d_10" `"{&Delta} ln(Top 10% / Ind Sales)"' "Y_ln_saless_ind_d_10_X_ln_mkt_d"
fig_sum "1" "ln_saless_ind_d_1" `"{&Delta} ln(Top 1% / Ind Sales)"' "Y_ln_saless_ind_d_1_X_ln_mkt_d"
fig_sum "10" "sales_hhi_ind_d" `"{&Delta} Sales HHI"' "Y_sales_hhi_ind_d_X_ln_mkt_d"
fig_sum "10" "ln_sales_hhi_ind_d" `"{&Delta} ln(Sales HHI)"' "Y_ln_sales_hhi_ind_d_X_ln_mkt_d"

f_reg_save "fr_reg" "${dir_outf}/reg_mkt_d_ind_con"

//==============================================================================
//==============================================================================
// Deciles

f_reg_create "fr_reg" "1"

do ${dir_do}/mkt_d_ind_0.do "geo"

foreach iperc in 10 20 30 40 50 60 70 80 90 100 {
fig_sum "10" "ln_emps_ind_d_`iperc'd" `"{&Delta} ln(Top `=`iperc'-10'-`iperc'% / Ind Emp)"' "Y_ln_emps_ind_d_`iperc'd_X_ln_mkt_d" "0"
fig_sum "10" "ln_pays_ind_d_`iperc'd" `"{&Delta} ln(Top `=`iperc'-10'-`iperc'% / Ind Pay)"' "Y_ln_pays_ind_d_`iperc'd_X_ln_mkt_d" "0"
}

//==============================================================================
// Sales

do ${dir_do}/mkt_d_ind_0.do "geo" "" "sales"

foreach iperc in 10 20 30 40 50 60 70 80 90 100 {
fig_sum "10" "ln_saless_ind_d_`iperc'd" `"{&Delta} ln(Top `=`iperc'-10'-`iperc'% / Ind sales)"' "Y_ln_saless_ind_d_`iperc'd_X_ln_mkt_d" "0"
}

f_reg_save "fr_reg" "${dir_outf}/reg_mkt_d_ind_con_decile"

di "Ended at $S_DATE $S_TIME"
capture log close
// End of do file
